package com.example.toutiao.ui.main.act

import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.widget.Toast
import androidx.core.content.ContextCompat
import com.example.toutiao.R
import com.example.toutiao.base.BaseActivity
import com.example.toutiao.base.Constants
import com.example.toutiao.ui.main.model.RegisterModel
import com.example.toutiao.ui.main.presenter.RegisterPresenter
import com.example.toutiao.ui.main.view.RegisterView
import com.example.toutiao.ui.user.EvtMsgModel
import com.example.toutiao.utils.CommonUtils
import kotlinx.android.synthetic.main.activity_find_pass.*

class FindPassActivity : BaseActivity<RegisterView, RegisterPresenter>(), RegisterView,
    TextWatcher {
    private var phone:String =""
    override fun getLayoutId(): Int = R.layout.activity_find_pass

    override fun initView() {
        cd_next.setCardBackgroundColor(ContextCompat.getColor(this, R.color.c_222222))
        login_mobile.addTextChangedListener(this)
        cd_next.setOnClickListener {
            if(TextUtils.isEmpty(phone)){
                Toast.makeText(application,"请先填写电话号码", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            val intent= Intent(this@FindPassActivity,SendCodeActivity::class.java)
            intent.putExtra(Constants.MOBILE,phone)
            intent.putExtra(Constants.USER_ACTION, Constants.ACTION_FIND_PASS)
            startActivity(intent)
        }
        icon_back.setOnClickListener {
            finish()
        }

    }

    override fun initData() {

    }

    override fun createPresenter(): RegisterPresenter =RegisterPresenter()



    override fun onRegister(registerModel: RegisterModel) {

    }

    override fun <T> setData(data: T) {

    }

    override fun setError(err: String) {

    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        phone= CommonUtils.replaceBlank(s.toString())
        checkPhone()
    }

    private fun checkPhone(){
        if(phone.length==11){
            cd_next.setCardBackgroundColor(ContextCompat.getColor(this, R.color.main_color))
            cd_next.isEnabled=true
        }else{
            cd_next.setCardBackgroundColor(ContextCompat.getColor(this, R.color.c_222222))
            cd_next.isEnabled=false
        }
    }
    override fun afterTextChanged(s: Editable?) {

    }
    override fun isRegister(): Boolean =true
    override fun getMessage(message: EvtMsgModel<*>) {
        super.getMessage(message)
        if(message!=null){
            if(message.code==103){
                finish()
            }
        }
    }
}